﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="GetFiles.cs" company="Collaboris Ltd.">
//   Copyright (c) Collaboris Ltd. All rights Reserved.
// </copyright>
// <summary>
//   The get files.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Collaboris.Saf.Actions.Files
{
    using System;
    using System.Collections.Generic;
    using System.IO;

    using Engine;

    using SharePoint.Wss.Attributes;

    [CollaborisClass(
          Title = "Get Files",
          ReleaseStatus = ReleaseStatus.Stable,
          DisableDocumentation = false,
          ContactAuthorEmail = "Support@Collaboris.co.uk",
          Summary = "Gets a list of file names (with path) for the given criteria.",
          Remarks = "The list of filenames is stored in a property within the entity called 'FileNames'. " +
                    "This can then be passed into subsequent actions using output properties.",
          ClassGuid = "{156E17A5-A7BA-4e1f-8A48-1BC341A7C613}"

      )
    ]
    [Serializable]
    public class GetFiles : ActionBase<Entities.FileInfo>
    {
        /// <summary>
        /// Executes the Action using the values passed in by the Data Entity
        /// and the <see cref="SafContext"/>.
        /// </summary>
        /// <param name="context">
        /// A valid <see cref="SafContext"/> containing properties to run the Action with.
        /// </param>
        /// <param name="entity">
        /// A Valid <see cref="T"/> containing the parameters with which to run the Action with.
        /// </param>
        /// <returns>
        /// A <see cref="ProcessStatus"/> signifying the outcome of processing the Action.
        /// </returns>
        protected override ProcessStatus DoAction(SafContext context, Entities.FileInfo entity)
        {
            if (! Directory.Exists(entity.Path))
            {
                throw new DirectoryNotFoundException(entity.Path);
            }

            string[] files = Directory.GetFiles(entity.Path, entity.SearchPattern, entity.SearchOption);
            
            if (files.Length > 0)
            {
                entity.FileNames = new List<string>();
                foreach (string s in files)
                {
                    entity.FileNames.Add(s);
                }
            }
            return ProcessStatus.Success;
        }


        /// <summary>
        /// Checks the Data entity used by the Action for Validity
        /// </summary>
        /// <param name="context">
        /// The <see cref="SafContext"/> passed in by the SAF Engine.
        /// </param>
        /// <param name="entity">
        /// The data entity to Validate.
        /// </param>
        /// <param name="errors">
        /// List of <see cref="ValidationError"/>s
        /// </param>
        /// <param name="mode">
        /// <see cref="ProcessMode"/>signalling wheter we are Doing, or Undoing the action.
        /// </param>
        /// <returns>
        /// false if invalid; otherwise true.
        /// </returns>
        protected override bool IsValid(
                SafContext context, Entities.FileInfo entity, ref List<ValidationError> errors, ProcessMode mode)
        {
            this.Trace.TraceMethodStart("IsValid");

            int currentCount = errors.Count;

            // Validate each object now.
            ValidateStringForNull(entity.SearchPattern, "FileInfo.SearchPattern", ref errors);
            ValidateStringForNull(entity.Path, "FileInfo.Path", ref errors);

            this.Trace.TraceMethodEnd("IsValid");

            // Have we added errors ?
            return errors.Count == currentCount;
        }
    }
}